package cate.game.play.skill.passive.awaken;

import cate.common.table.d.GDSkill;
import cate.game.play.base.TargetFilterParam;
import cate.game.play.base.TargetSelector;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.proce.action.ctx.SkillActionCtx;
import cate.game.play.skill.passive.PassiveHandler;

/**
 * 效果1：进入觉醒状态时驱散自身2个负面状态;觉醒状态下每次受到攻击提升自身2.2%伤害减免(不可驱散),每回合至多2次,最多10层;
 *        每次受击都可获得1层【灵盾】,每回合至多2层，下回合将所有灵盾给任意友军,提升目标25%效率的伤害减免1回合,每个英雄至多获得1层【灵盾】
 * 效果2：【觉醒·永固】额外提升20%灵能抵抗;觉醒状态下任意敌方单位死亡则随机提升1名友方单位25%觉醒值,任意友军死亡则降低1名敌方单位25%觉醒值,每回合总共可触发3次
 * 效果3：进入觉醒状态时为自身和1名友方单位附加2层【真理守护】,
 */
public class 真理守护PH extends PassiveHandler {

	private int 驱散数量;

	private int 减伤buff;
	private int 减伤buff数量;

	private int 灵盾buff;
	private int 灵盾buff数量;
	private int 友军buff;

	private double 友方提升;
	private double 敌方减少;
	private double 触发次数;

	private int 真理守护buff;

	//驱散数量=2&减伤buff=？？&减伤buff数量=2&灵盾buff=？？&灵盾buff数量&友军buff=？？&
	//友方提升=25&敌方减少=25&触发次数=3&
	//真理守护buff=？？
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		驱散数量 = args.getInt("驱散数量", 0);
		减伤buff = args.getInt("减伤buff", 0);
		减伤buff数量 = args.getInt("减伤buff数量", 0);

		灵盾buff = args.getInt("灵盾buff", 0);
		灵盾buff数量 = args.getInt("灵盾buff数量", 0);
		友军buff = args.getInt("友军buff", 0);

		友方提升 = args.getDouble("友方提升", 0d);
		敌方减少 = args.getDouble("敌方减少", 0d);
		触发次数 = args.getInt("触发次数", 0);

		真理守护buff = args.getInt("真理守护buff", 0);
	}

	@Override
	public void onAwaken(ActionCtx action, boolean start) {
		if (start) {
			if (驱散数量 > 0) {
				skill.owner.buff.tryDispelBad(action, 驱散数量);
			}
			if (真理守护buff > 0 ) {
				skill.owner.buff.tryAddByTid(action, 真理守护buff);
				Fighter one = TargetSelector.findOne(skill.owner, action.getTeamVs(), new TargetFilterParam().scopeSet(GDSkill.Scope.FRIENDS));
				if (one != null) {
					one.buff.tryAddByTid(action, skill.owner, 真理守护buff);
				}
			}
		}
	}

	@Override
	public void onRoundBegin(ActionCtx action) {
		attrBuffAdd = 0;
		if (shieldBuffAdd > 0) {
			int remove = skill.owner.buff.removeBuffByTid(action, 灵盾buff);
			if (remove > 0) {
				TargetFilterParam param = new TargetFilterParam().scopeSet(GDSkill.Scope.FRIENDS).targetNumSet(shieldBuffAdd);
				for (Fighter one : TargetSelector.doFind(skill.owner, action.getTeamVs(), param)) {
					one.buff.tryAddByTid(action, skill.owner, 友军buff);
				}
			}
			shieldBuffAdd = 0;
		}
		addEnergy = 0;
	}

	private int attrBuffAdd;
	private int shieldBuffAdd;

	@Override
	public void afterSufferOneAttack(SkillActionCtx action, long totalDamage, boolean strike) {
		if (!skill.owner.status.isAwaken()) {
			return;
		}
		if (attrBuffAdd < 减伤buff数量) {
			attrBuffAdd++;
			skill.owner.buff.tryAddByTid(action, 减伤buff);
		}

		if (shieldBuffAdd < 灵盾buff数量) {
			shieldBuffAdd++;
			skill.owner.buff.tryAddByTid(action, 灵盾buff);
		}
	}

	private int addEnergy;

	@Override
	public void onOtherDead(ActionCtx action, Fighter killer, Fighter deader) {
		if (skill.owner.isDead() || !skill.owner.status.isAwaken()) {
			return;
		}
		if (addEnergy >= 触发次数) {
			return;
		}
		Fighter one;
		if (deader.getTeam() == skill.owner.getTeam()) {
			one = TargetSelector.findOne(skill.owner, action.getTeamVs(), new TargetFilterParam());
			if (one != null) {
				one.skill.addAwakenEnergy(action, -敌方减少);
				addEnergy++;
			}
		} else {
			one = TargetSelector.findOne(skill.owner, action.getTeamVs(), new TargetFilterParam().scopeSet(GDSkill.Scope.FRIENDS));
			if (one != null) {
				one.skill.addAwakenEnergy(action, 友方提升);
				addEnergy++;
			}
		}

	}
}
